import { Form, Input, Select } from "antd";
import type { QuestionTitlePropsType } from "./interface";
import { useEffect } from "react";
import Checkbox from "antd/es/checkbox/Checkbox";

export default function PropComponent(props: QuestionTitlePropsType) {
  const { text, level, isCenter, onChange } = props;
  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue({ text, level, isCenter });
  }, [text, level, isCenter]);
  function handleValueChange() {
    if (onChange) {
      onChange(form.getFieldsValue());
    }
  }
  return (
    <Form
      layout="vertical"
      initialValues={{ text, level, isCenter }}
      form={form}
      onValuesChange={handleValueChange}
    >
      <Form.Item
        label="标题内容"
        name="text"
        rules={[
          {
            required: true,
            message: "请输入标题内容",
          },
        ]}
      >
        <Input />
      </Form.Item>
      <Form.Item label="层级" name="level">
        <Select
          options={[1, 2, 3].map((value) => {
            return {
              value,
              text: value,
            };
          })}
        ></Select>
      </Form.Item>
      <Form.Item name="isCenter" valuePropName="checked">
        <Checkbox>居中显示</Checkbox>
      </Form.Item>
    </Form>
  );
}
